package com.demo.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.common.Result;
import com.demo.entity.Achievement;
import com.demo.entity.Paper;
import com.demo.mapper.AchievementMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 科研成果
 */
@RestController
@RequestMapping("/achievement")
public class AchievementController {
  /**
   * 服务对象
   */
  @Resource
  AchievementMapper achievementMapper;
  
  // 新增
  @PostMapping
  public Result<?> insert(@RequestBody Achievement achievement) {
    System.out.println(achievement);
    try {
      achievementMapper.insert(achievement);
      return Result.success();
    } catch (Exception e) {
      System.out.println(e);
      return Result.error("201", "");
    }
  }
  
  
/*  // 新增
  @PostMapping
  public Result<?> insert(@RequestBody Paper paper) {
    try {
      paperMapper.insert(paper);
      return Result.success();
    } catch (Exception e) {
      return Result.error("201","");
    }
    
  }*/
  
  //  删除 ById
  @DeleteMapping("/{id}")
  public Result<?> delete(@PathVariable Integer id) {
    achievementMapper.deleteById(id);
    return Result.success();
  }
  
  // 修改
  @PutMapping
  public Result<?> updata(@RequestBody  Achievement achievement) {
    try {
      achievementMapper.updateById(achievement);
      return Result.success();
    } catch (Exception e) {
      return Result.error("201", "");
    }
   
  }
  
  /**
   * 分页,默认1页,10个数据,  默认根据 Aname(成果名称) 查询
   *
   * @param pageNum  页数
   * @param pageSize 页面显示数
   * @param search   搜索关键词
   * @param sort     搜索类别
   * @return
   */
  @GetMapping
  public Result<?> findPageByaName(@RequestParam(defaultValue = "1") Integer pageNum,
                                   @RequestParam(defaultValue = "10") Integer pageSize,
                                   @RequestParam(defaultValue = "") String search,
                                   @RequestParam(defaultValue = "Aname") String sort) {
    LambdaQueryWrapper<Achievement> wrapper = Wrappers.<Achievement>lambdaQuery();
    //根据Id逆序排列
    wrapper.orderByDesc(Achievement::getId);
    if (StrUtil.isNotBlank(search)) {
      switch (sort) {
        case "Aname": //根据成果名称搜索
          wrapper.like(Achievement::getAname, search);
          break;
        case "Acategory"://根据成果类型搜索
          wrapper.like(Achievement::getAcategory, search);
          break;
        case "Aleader"://负责人
          wrapper.like(Achievement::getAleader, search);
          break;
        case "Finishdep"://完成单位
          wrapper.like(Achievement::getFinishdep, search);
          break;
      }
    }
    
    Page<Achievement> AdminPage = achievementMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
    return Result.success(AdminPage);
  }
  
  // 根据name 查询是否有这个主键
  @GetMapping("/aname/{aname}")
  public Result<?> getName(@PathVariable String aname) {
    Achievement achievement = achievementMapper.selectOne(Wrappers.<Achievement>lambdaQuery().eq(Achievement::getAname, aname));
    if(achievement == null ){
      //表示不含有此 主键,可以通过
      return Result.success();
    }
    //表示表中已存在此 主键,不能通过
    return Result.errorData("201", achievement);
  }
  
}
